﻿/* main.c */
#include "app.h"

/*******************************************************************************
 * Definitions
 ******************************************************************************/

#define APP_NN_CLASS_NUM              NN_VECTOR_LENGTH_MAX

/*******************************************************************************
 * Variables
 ******************************************************************************/
uint32_t tick_counter_val;

float app_sigmoid_input[NN_VECTOR_LENGTH_MAX];
float app_sigmoid_output[NN_VECTOR_LENGTH_MAX];

float app_softmax_input[APP_NN_CLASS_NUM];
float app_softmax_output[APP_NN_CLASS_NUM];

/*******************************************************************************
 * Prototypes
 ******************************************************************************/
void app_nn_test_sigmoid_pq(void);
void app_nn_test_sigmoid_arm(void);
void app_nn_test_softmax_pq(void);
void app_nn_test_softmax_arm(void);
void app_nn_test_tanh_pq(void);
void app_nn_test_tanh_arm(void);

/*******************************************************************************
 * Code
 ******************************************************************************/
/*!
 * @brief Main function
 */
int main(void)
{
    BOARD_InitHardware();

    PRINTF("\r\nPowerQuad NN Example.\r\n\r\n");

    /* 生成输入数据. */
    for (uint32_t i = 0; i < NN_VECTOR_LENGTH_MAX; i++)
    {
        app_sigmoid_input[i] = (i * 12.0f / NN_VECTOR_LENGTH_MAX) - 6.0f;
    }
    for (uint32_t i = 0; i < APP_NN_CLASS_NUM; i++)
    {
        app_softmax_input[i] = i * 0.1 - 0.5;
    }

    nn_init();

    while (1)
    {
        GETCHAR();
        app_nn_test_sigmoid_pq();
        app_nn_test_sigmoid_arm();
        PRINTF("\r\n");

        GETCHAR();
        app_nn_test_softmax_pq();
        app_nn_test_softmax_arm();
        PRINTF("\r\n");

        GETCHAR();
        app_nn_test_tanh_pq();
        app_nn_test_tanh_arm();
        PRINTF("\r\n");
    }
} /* end_main. */

void app_nn_test_sigmoid_pq(void)
{
    PRINTF("%s()\r\n", __func__);

    memset(app_sigmoid_output, 0, sizeof(float) * NN_VECTOR_LENGTH_MAX);

    TimerCount_Start(); /* 开始计时 */
    nn_layer_activate_sigmoid_pq(app_sigmoid_input, app_sigmoid_output, NN_VECTOR_LENGTH_MAX);
    TimerCount_Stop(tick_counter_val);

    /* 打印计算结果. */
    for (uint32_t i = 0; i < NN_VECTOR_LENGTH_MAX; i+=16)
    {
        PRINTF("[%3d] input: %8f, output:%8f\r\n",
             i, app_sigmoid_input[i], app_sigmoid_output[i]);
    }
    PRINTF("done with %d us\r\n", tick_counter_val/96);
}

void app_nn_test_sigmoid_arm(void)
{
    PRINTF("%s()\r\n", __func__);

    memset(app_sigmoid_output, 0, sizeof(float) * NN_VECTOR_LENGTH_MAX);

    TimerCount_Start(); /* 开始计时 */
    nn_layer_activate_sigmoid_arm(app_sigmoid_input, app_sigmoid_output, NN_VECTOR_LENGTH_MAX);
    TimerCount_Stop(tick_counter_val);

    /* 打印计算结果. */
    for (uint32_t i = 0; i < NN_VECTOR_LENGTH_MAX; i+=16)
    {
        PRINTF("[%3d] input: %8f, output:%8f\r\n",
             i, app_sigmoid_input[i], app_sigmoid_output[i]);
    }
    PRINTF("done with %d us\r\n", tick_counter_val/96);
}

void app_nn_test_softmax_pq(void)
{
    PRINTF("%s()\r\n", __func__);

    memset(app_softmax_output, 0, sizeof(float) * APP_NN_CLASS_NUM);

    TimerCount_Start();
    nn_layer_activate_softmax_pq(app_softmax_input, app_softmax_output, APP_NN_CLASS_NUM);
    TimerCount_Stop(tick_counter_val);

    for (uint32_t i = 0; i < APP_NN_CLASS_NUM; i+=16)
    {
        PRINTF("[%3d] input: %8f, output:%8f\r\n",
             i, app_softmax_input[i], app_softmax_output[i]);
    }
    PRINTF("done with %d us\r\n", tick_counter_val/96);
}

void app_nn_test_softmax_arm(void)
{
    PRINTF("%s()\r\n", __func__);

    memset(app_softmax_output, 0, sizeof(float) * APP_NN_CLASS_NUM);

    TimerCount_Start();
    nn_layer_activate_softmax_arm(app_softmax_input, app_softmax_output, APP_NN_CLASS_NUM);
    TimerCount_Stop(tick_counter_val);

    for (uint32_t i = 0; i < APP_NN_CLASS_NUM; i+=16)
    {
        PRINTF("[%3d] input: %8f, output:%8f\r\n",
             i, app_softmax_input[i], app_softmax_output[i]);
    }
    PRINTF("done with %d us\r\n", tick_counter_val/96);
}

void app_nn_test_tanh_pq(void)
{
    PRINTF("%s()\r\n", __func__);

    memset(app_sigmoid_output, 0, sizeof(float) * NN_VECTOR_LENGTH_MAX);

    TimerCount_Start();
    nn_layer_activate_tanh_pq(app_sigmoid_input, app_sigmoid_output, NN_VECTOR_LENGTH_MAX);
    TimerCount_Stop(tick_counter_val);

    for (uint32_t i = 0; i < NN_VECTOR_LENGTH_MAX; i+=16)
    {
        PRINTF("[%3d] input: %8f, output:%8f\r\n",
             i, app_sigmoid_input[i], app_sigmoid_output[i]);
    }
    PRINTF("done with %d us\r\n", tick_counter_val/96);
}

void app_nn_test_tanh_arm(void)
{
    PRINTF("%s()\r\n", __func__);

    memset(app_sigmoid_output, 0, sizeof(float) * NN_VECTOR_LENGTH_MAX);

    TimerCount_Start();
    nn_layer_activate_tanh_arm(app_sigmoid_input, app_sigmoid_output, NN_VECTOR_LENGTH_MAX);
    TimerCount_Stop(tick_counter_val);

    for (uint32_t i = 0; i < NN_VECTOR_LENGTH_MAX; i+=16)
    {
        PRINTF("[%3d] input: %8f, output:%8f\r\n",
             i, app_sigmoid_input[i], app_sigmoid_output[i]);
    }
    PRINTF("done with %d us\r\n", tick_counter_val/96);
}

/* EOF. */

